
module Fighter {
	/**
	 * 储存所有的飞机
	 * （1）根据服务器每波的数据生成飞机。
	 * （2）将飞机漏兵数据进行保存，发给防守界面，转换后发给服务器。
	 * （3）驱动所有存活的飞机移动
	 * */
	export class FlyManager {
		/**初始化飞机层 */
		constructor(tshowpanle) {
			this.showPanle = tshowpanle;
		}

		/**对象池存储字典 */
		private Leakflys = [];
		/**子弹层 */
		public bulletManager;
		/**对象池存储字典 */
		public productflys: BasicFly[] = []
		/**显示容器 */
		private showPanle: egret.DisplayObjectContainer;

		/**创建飞机的个数，用来作为飞机的ID */
		private static productFlyNumbers = 0;


		/**创建指定类型和位置的飞机 */
		public createFly(type, point) {
			//console.log("boshu"+GameUtil.PlayerMessage.instance.game_turns);

			let fly = GameUtil.GamePools.creatGamePools().producePlane(type);
			// let fly: any;
			// switch (type) {
			// 	case 0:
			// 		fly = new Fighter.BasicFly();
			// 		break;
			// 	case 1:
			// 		fly = new Fighter.PlaneAddSpeed();
			// 		break;
			// 	case 2:
			// 		fly = new Fighter.PlaneSelfDestruction();
			// 		break;
			// 	case 3:
			// 		fly = new Fighter.PlaneShield();
			// 		break;
			// 	case 4:
			// 		fly = new Fighter.PlaneShot();
			// 		break;
			// 	default:
			// 		console.error("不存在" + type + "型飞机");
			// 		break;
			// }			

			this.productflys.push(fly);
			fly.init(this.showPanle, this, type);
			fly.born(point, FlyManager.productFlyNumbers);
			FlyManager.productFlyNumbers++;
		}

		/**从对象池中销毁一架 */
		public destoryOneFly(tid) {
			for (var i = this.productflys.length - 1; i >= 0; i--) {
				if (this.productflys[i].id == tid) {
					//删除飞机
					//GameUtil.GamePools.instance.reclaim(this.productflys[i]);
					this.productflys.splice(i, 1);
					return;
				}
			}

		}
		/**回合结束 
		 * 1.判断是否有剩余的飞机，有的话保存发给服务器
		*/
		public fightRoundEnd() {
			//GameUtil.PlayerMessage.instance.Game2addEnemy = this.productflys.slice(0);
			for (let i = 0; i < this.productflys.length; i++) {
				GameUtil.PlayerMessage.instance.Game2addEnemy.push(this.productflys[i].type);
			}
			while (this.productflys.length > 0) {
				this.productflys[this.productflys.length - 1].destroy();
			}
		}

		/**根据数组发放飞机 */
		public fightRoundStart(flyList: number[]) {
			this.createBornPoint(flyList);
			for (let i = 0; i < flyList.length; i++) {
				let fly = flyList[i];

				let point;
				if (this.showPanle.name == "Defense") {
					point = GameUtil.PlayerMessage.instance.defensePosition[i];
				}
				else if (this.showPanle.name == "EnemyDefense") {
					point = GameUtil.PlayerMessage.instance.enemyDefensePosition[i];
				}
				else {
					console.error("未生成飞机出发点");
				}
				if (!GameUtil.PlayerMessage.instance.onePerson) {
					point = new egret.Point(point.positionX, -i * 200);
				}
				if (fly != null)
					this.createFly(fly, point);
			}
			flyList.splice(0);

		}
		/**飞机每帧移动 */
		public update(deltaTimeStamp) {
			for (var i = 0; i < this.productflys.length; i++) {
				let fly = this.productflys[i];
				if (fly.isLiving) {
					fly.update(deltaTimeStamp);
				}
			}
		}
		/**防守界面单机时生成飞机出生点 */
		private createBornPoint(list: any[]) {
			if (GameUtil.PlayerMessage.instance.onePerson) {
				GameUtil.PlayerMessage.instance.defensePosition.splice(0);
				GameUtil.PlayerMessage.instance.enemyDefensePosition.splice(0);
				for (let i = 0; i < list.length; i++) {
					let point = new egret.Point();
					point.x = i % 3 * 640 / 3;
					point.y = 0 - i * 3 * 50;
					GameUtil.PlayerMessage.instance.defensePosition.push(point);
					GameUtil.PlayerMessage.instance.enemyDefensePosition.push(point);
				}
			}
		}








	}
}